<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>运单导入</title>
</head>

<body>
	<div id="fullbg"></div>
	<div id="dialog">
		<div>
			<img src="${ctx }/static/022.gif"></img>
		</div>
	</div>
	<section class="content">
		<div class="row">
			<div class="col-md-12">
				<!-- Primary box -->
				<div class="box box-solid box-primary">
					<div class="box-header">
						<i class="fa fa-search"></i>
						<h3 class="box-title">搜索栏</h3>
						<div class="box-tools pull-right">
							<button class="btn btn-primary btn-sm" data-widget="collapse">
								<i class="fa fa-minus"></i>
							</button>
						</div>
					</div>
					<div class="box-body">
						<form class="form-search" id="queryForm"  >
						    <input id="_orderbystatus" name="_orderbystatus"  value="status" type="hidden"/>
							<div class="form-group row">
								<div class="col-sm-3">
									<div class="input-group col-sm-12">	
										<label class="input-group-text">商家名称:</label>
									   	<select  name="customer_name"  id="_customerName" name="_customerName"  class="form-control" >
									   		<option value=""></option>
										<c:forEach items="${customerNameList}" var="k">
											<option value="${k}"
													<c:if test="${customerName eq k}">selected=selected</c:if>>${k}</option>
										</c:forEach>	
										</select>
									</div>
								</div>
								<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<label  class="input-group-text" >收件省:</label>
										<select  name="save_country"  id="_toProvince" name="_toProvince"  class="form-control" >
											<option value=""></option>
											<c:forEach items="${toProvinceList}" var="k">
												<option value="${k}"
													<c:if test="${toProvince eq k}">selected=selected</c:if>>${k}</option>
											</c:forEach>
										</select>
									</div>
		    					</div>
		 						<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<label  class="input-group-text" >运单编号:</label>
										<input type="text" id="_expressNo" class="form-control" name="_expressNo" />
									</div>
		    					</div>
		    					<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<label  class="input-group-text" >订单编号:</label>
										<input type="text" id="_orderNo" class="form-control" name="_orderNo" />
									</div>
		    					</div>
		    				</div>
		    				
		    				
		    				<div class="form-group row">
		    					<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<label  class="input-group-text" >收件人姓名:</label>
										<input type="text" id="_toName" class="form-control" name="_toName" />
									</div>
		    					</div>
		    					
		    					<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<span class="input-group-text">创建时间 :</span> 
										<div class="input-group-addon"> 
											<i class="fa fa-calendar"></i>
										</div> 
										<input type="text" name="_fromDate" id="_fromDate"  class="form-control span3"  value="${param._fromDate}"  />
										
									</div>
								</div>
		    					<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<span class="input-group-text">至： </span> 
										<div class="input-group-addon"> 
											<i class="fa fa-calendar"></i>
										</div> 
										<input type="text" name="_toDate" id="_toDate" class="form-control required" value="${param._toDate}" />
										
									</div>
								</div>
				
								<div class="col-sm-3">
									<div class="input-group col-sm-12">	
			    						<button type="button" onclick="query();" class="btn btn-info" >搜索</button>
			 						</div>
								</div>
		    				</div>
	   					</form>
	   					<button class="btn btn-success" id="import" onclick="showExpress('leadExpressBillManage');">导入运单</button>
			    		<button class="btn btn-success" id="importSet" onclick="showExpress('getExcelTop');">导入模板设置</button>
					</div>
				</div>
			</div>
		</div>
	
		<div class="form-group row">
			<div class="col-md-12">
				<div class="box box-solid box-primary">
					<div class="box-header">
						<i class="fa fa-list"></i>
						<h3 class="box-title">运单列表</h3>
						<div class="box-tools pull-right">
							<button class="btn btn-primary btn-sm" data-widget="collapse" type="button">
								<i class="fa fa-minus"></i>
							</button>
						</div>
					</div>
					<div class="box-body">
						<table id="contentTable" class="table table-striped table-bordered table-condensed">
							<thead>
								<tr>
								    <th>序号</th>
									<th>商家名称</th>
									<th>运单编号</th>
									<th>订单编号</th>
									<th>收件人姓名</th>
									<th>收件人电话</th>
									<th>收件省</th>
									<th>下单时间</th>
									<th>发件人姓名</th>
									<th>发件人电话</th>
									<th>重量</th>
									<th>运单详情</th>
								</tr>
							</thead>
							<tbody>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	
		<!-- 弹出窗体-导入运单 start -->
		<div class="modal fade import-excel" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
			<div class="modal-dialog" style="width: 60%;">
				<div class="modal-content">
					<div class="modal-body">
						<div class="box box-solid box-primary">
				      		<div class="box-header" id="importTitle"><span class="box-title">导入运单信息</span></div>
							<div class="box-body">
								<form id="form1"  method="post" enctype="multipart/form-data">
									<table class="table table-striped table-bordered table-condensed">
										<tbody>
											<tr>
												<td>
													<input name="file" id="file" class="btn required"  type="file"/>
												</td>
											</tr>
											<tr>
												<td style="height: 50px; vertical-align: middle;">
													<input id="submit_btn" class="btn btn-primary" type="button" onclick="fullbgDialog();" value="提交" />
													<input type="reset" class="btn"  />
												</td>
											</tr>
									  </tbody>
								   </table>
							   </form>
							</div>
						</div>
					</div>	
				</div>	
			</div>
		</div>
		<!-- 弹出窗体-导入运单 end  -->

		<!-- 运单详情弹出框 -->
		<div class="modal fade send-info" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" id="showBillDetail">
	  		<div class="modal-dialog modal-lg">
	    		<div class="modal-content">
	      			<div class="modal-body list-group" id="billDetail">
	      				
	      			</div>
	      			<div class="modal-footer">
	        			<button class="btn btn-primary" data-dismiss="modal" aria-label="Close">关闭</button>
	      			</div>
	    		</div>
	  		</div>
		</div>
		
		<div class="modal fade send-info1" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" id="showMessage">
	  		<div class="modal-dialog modal-lg">
	    		<div class="modal-content">
	      			<div class="modal-body list-group" id="billDetail">
	      				<div class="box box-solid box-primary">
	      				<h4 class="box-header" id="gridSystemModalLabel">运单详情</h4>
	      				</div>
	      		
	      				<div class="row list-group-item">
	      				<div class="col-md-3">成功数量:</div>
	      				<div class="col-md-9">${normalSize }条</div>
	      				</div>
	      				<div class="row list-group-item">
	      				<div class="col-md-3">失败数量:</div>
	      				<div class="col-md-9">${exceptionSize }条</div>
	      				</div>
	      			</div>
	      			<div class="modal-footer">
	        			<button class="btn btn-primary" data-dismiss="modal" aria-label="Close">关闭</button>
	      			</div>
	    		</div>
	  		</div>
		</div>
</section>	
<script type="text/javascript">
	var dataTables_expressBill;
	var erpType ="setExpressBillMould";
    var customers ="STO";
	//查询
	function query(){
		if($("#queryForm").valid()){
			if (dataTables_expressBill) {
				dataTables_expressBill.fnDestroy();
			}
			initTable();
		}
	}
	 
	//分页查询结果
	function initTable(){
		//初始化dataTables-快递单流量信息
	    dataTables_expressBill=$("#contentTable").dataTable({
           "bServerSide":true,
           "sAjaxSource":'${ctx}/v2/expressBillManage/loadData',
           "fnServerData": loadData_unhandle,
           "fnRowCallback":rowCallBack,
           "iDisplayLength": 10, //每页数据
           "bPaginate": true, //翻页功能
           "bLengthChange": true, //改变每页显示数据数量
           "bFilter": false, //过滤功能
           "bSort": false, //排序功能
           "bInfo": true,//页脚信息
           "bAutoWidth": true,//自动宽度
           "bStateSave": false, //状态保存，使用了翻页或者改变了每页显示数据数量，会保存在cookie中，下回访问时会显示上一次关闭页面时的内容
           "bProcessing": true, //加载数据时显示正在加载信息
           
           "aoColumns": [  //排序控制
				null,
				{ "asSorting": [ "asc" ] },
				{ "asSorting": [ "desc", "asc", "asc" ] }
			],
           
           "sZeroRecords": "查询无记录！",
           "oLanguage": { //汉化   
               "sUrl": '${ctx}/static/v2/js/plugins/datatables/cn.txt'
            },
            "aoColumns": [ 
                    {"mData": null},
                    {"mData": 'customerName'}, 
                    {"mData": 'expressNo'},
                    {"mData": 'orderNo'},
                    {"mData": 'toName'},
                    {"mData": 'toTele'},
                    {"mData": 'toProvince'},
                    {"mData": 'orderTime',
                    	 "render": function (obj) {
	                    	  var time=obj.time;
	                         var date = new Date(time);
	                         return date.Format("yyyy-MM-dd hh:mm");
	                      }
                    },
                    {"mData": 'fromName'},
                    {"mData": 'fromTele'},
                    {"mData": 'weight'},
                   	{"mData": null}
             ],
            "aoColumnDefs":[
   	             	{"aTargets": [11],
   	             		"render": function (obj,type,full) {
   							return "<a href='#showBillDetail' onclick='getBillDetail("+full.id+")' class='btn btn-default btn-sm sendInfo' role='button' data-toggle='modal' >订单详情</a>";
   			           }
   		            },
           	   ]         	
        });
	   return dataTables_expressBill;
	}
	
	//创建了行的回调 nRow为当前的tr
	function rowCallBack(nRow, aData, iDisplayIndex, iDisplayIndexFull){
		var iDisplayStart = dataTables_expressBill.fnSettings()._iDisplayStart;
		$("td:eq(0)", nRow).html(iDisplayStart+iDisplayIndex+1);
	}
	//回调函数_待处理订单
	function loadData_unhandle( sSource, aoData, fnCallback ) {
	   //查询参数
	   aoData.push( { "name": "customerName", "value": $("#_customerName").val() } );  
	   aoData.push( { "name": "toName", "value": $("#_toName").val() } );
	   aoData.push( { "name": "fromDate", "value": $("#_fromDate").val() } );
	   aoData.push( { "name": "toDate", "value": $("#_toDate").val() } );
	   aoData.push( { "name": "orderNo", "value": $("#_orderNo").val() } );
	   aoData.push( { "name": "toProvince", "value": $("#_toProvince").val() } );
	   aoData.push( { "name": "expressNo", "value": $("#_expressNo").val() } );
	   $.ajax({ 
	       "type": "post",
	       "contentType": "application/json", 
	       "url": sSource,    
	       "dataType": "json",   
	       "data": JSON.stringify(aoData),
	       "success": function(resp) {  
	       			fnCallback(resp);
	       			$("#total_unhandle").html("("+resp.iTotalRecords+")");
	       		}
	   });    
	}
	
	//检查是否有模板的设置
	function ajaxCheckExpressModel(param) {
		$.ajax({
			type : 'post',
			url : '${ctx}/v2/expressBillManage/ajaxCheckExpressModel',
			data : {
				customers:'STO',
				erpType:'setExpressBillMould'
			},
			dataType : 'json',
			success : function(data) {
				if(data.status=="1"){
					showImportExcel1(param);			
				}else if(data.status=="2"){
					showImportExcel2(param);
				}else{
					alert("获取导入模板异常");
				}
			},
			error:function() {
				alert("网络繁忙，请稍后再试...");
			}
		});
		
	}
	//存在导入模板 
	function showImportExcel1(methodParam){
		if(methodParam=="leadExpressBillManage"){//数据导入
			$("#importTitle").html("<span class='box-title'>导入运单信息</span>");
			showDialog(methodParam);
		}else if(methodParam=="getExcelTop"){	   //模板设置
			if(confirm("已存在模板，是否重新设置?")){
				$("#importTitle").html("<span class='box-title'>导入模板设置</span>");
				showDialog(methodParam);
			}
		}else{
			return;
		}
    }
	//不存在导入模板  
	function showImportExcel2(methodParam){
		if(methodParam=="leadExpressBillManage"){//数据导入			
			alert("未设置导入模板,请先配置模板!");
		}else if(methodParam=="getExcelTop"){	  //模板设置
			showDialog(methodParam);
			$("#importTitle").html("<span class='box-title'>导入模板设置</span>");
		}else{
			return;
		}
    }
	//显示导入对话框
	function showDialog(methodParam){
     	$('.import-excel').modal('show');	
		$("#form1").attr("action", "${ctx}/v2/expressBillManage/"+ methodParam +"/" + customers + "/" + erpType);
	}
	function fullbgDialog(){
		if($("#file").val()==""){
			alert("请选择文件！");
			return false;
		}else{
			$("#form1").submit();
			showBg();
		}
	}
	 //按钮点击事件
	function showExpress(param){
		 ajaxCheckExpressModel(param);
	}
	//获取运单详情
	function getBillDetail(id) {
		var tempString = "";
		$("#billDetail").html(tempString);
		$.ajax({
			type : 'post',
			url : '${ctx}/v2/expressBillManage/ajaxBillDetail',
			data : 'id=' + id,
			dataType : 'json',
			success : function(data) {
				if (data.status == "1") {
                    var date = new Date(data.expressBillManage.orderTime.time);
                     
					tempString += "<div class='box box-solid box-primary'>"+ "<h4 class='box-header' id='gridSystemModalLabel'>运单详情</h4>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>商家ID:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.customerId +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>商家名称:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.customerName +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>寄件网点:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.netWork +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>运单编号:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.expressNo +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>订单编号:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.orderNo +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>发件省:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.fromProvince +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>发件市:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.fromCity +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>发件区:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.fromDistrict +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>发件街道:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.fromStreet +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>发件详细地址:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.fromAddress +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>发件人姓名:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.fromName +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>发件人电话:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.fromTele +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>收件省:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.toProvince +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>收件市:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.toCity +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>收件区:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.toDistrict +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>收件街道:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.toStreet +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>收件详细地址:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.toAddress +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>收件人姓名:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.toName +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>收件人电话:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.toTele +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>下单时间:</div>" + "<div class='col-md-9'>"+ date.Format("yyyy-MM-dd hh:mm"); +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>重量:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.weight +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>体积:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.volume +"</div>" + "</div>"
					        +"<div class='row list-group-item'><div class='col-md-3'>大字:</div>" + "<div class='col-md-9'>"+ data.expressBillManage.headLine +"</div>" + "</div>";
					$("#billDetail").html(tempString);
				} else {
					alert("获取运单详细信息异常！");
				}
			},

			error : function() {
				alert("获取运单详细信息异常！");
			}
		});
	}
	
	//页面初始化
	$(document).ready(function(){
		    
		    $("#queryForm").validate(); 
			$("#_fromDate").datepicker({ format:'yyyy-mm-dd'});
			$("#_toDate").datepicker({ format:'yyyy-mm-dd'});
			var now = new Date();
			var tomorrow = new Date();
			tomorrow.setDate(now.getDate()+1);
			if(!$("#_fromDate").val()){
				$("#_fromDate").val(now.Format("yyyy-MM-dd"));
			}
			if($("#_toDate").val() == ''){
				$("#_toDate").val(tomorrow.Format("yyyy-MM-dd"));
			}
			
		    // 时间选择器
            $("#datepick input").datepicker({ 
            	keyboardNavigation: !1, 
            	forceParse: !1, 
            	autoclose: !0 
            });
        	
            // 重置运单
            $('#reload').click(function(e) {
            	e.preventDefault();
            	var file = $("#sendFile") ;
				file.after(file.clone().val(""));      
				file.remove();  
            	e.stopPropagation();
            })
            
            
            // 弹出运单详情
            $('table tbody').on('click','.sendInfo',function() {
            	$('.send-info').modal('show');
            })
		});
</script>
</body>
</html>
